package com.ws.game.lesson6
{
	import com.ws.game.lesson3.fire.KeyBoardCtrl;
	import com.ws.game.common.utils.MoveDirectionType;
	
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import mx.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	
	public class Key8Ctrl extends KeyBoardCtrl
	{
		private var _directions:ArrayCollection;
		public function Key8Ctrl()
		{
			super();
			_directions =new ArrayCollection();
		}
		
		override protected function onKeyDown(event:KeyboardEvent):void{		
//			super.onKeyDown(event);
			var me:Action8Object = _target as Action8Object;
			switch(event.keyCode)
			{
				case Keyboard.UP:				
				case Keyboard.DOWN:
				case Keyboard.LEFT:
				case Keyboard.RIGHT:					
					if(_directions.getItemIndex(event.keyCode) < 0 ){
						_directions.addItem(event.keyCode);
					}
					updateDirection();
					break;				
				case Keyboard.SPACE:
					me.shoot();
					break;
				default:break;
			}
		}
		
		override protected function onKeyUp(event:KeyboardEvent):void{
//			super.onKeyUp(event);
			var itemIndex:int=_directions.getItemIndex(event.keyCode);
			if(itemIndex >= 0){
				_directions.removeItemAt(itemIndex);
			}
			updateDirection();
		}
		
		private function updateDirection():void{
			var me:Action8Object = _target as Action8Object;
			var spanX:int=0;
			var spanY:int=0;
			
			if(_directions.getItemIndex(Keyboard.UP)>=0)
			{spanY++;}
			if(_directions.getItemIndex(Keyboard.DOWN)>=0)
			{spanY--;}
			if(_directions.getItemIndex(Keyboard.RIGHT)>=0)
			{spanX++;}
			if(_directions.getItemIndex(Keyboard.LEFT)>=0)
			{spanX--;}
			
			if(spanX == 0 && spanY == 0){
				me.isWalk =	false;
				me.direction = MoveDirectionType.STOP;				
			}else if(spanX > 0 && spanY==0){				
				me.isWalk =	true;
				me.direction = MoveDirectionType.RIGHT;
			}else if(spanX>0 && spanY>0){		
				me.isWalk =	true;
				me.direction = MoveDirectionType.RIGHT_UP;
			}else if(spanX == 0 && spanY>0){		
				me.isWalk =	true;
				me.direction = MoveDirectionType.UP;
			}else if(spanX<0 && spanY>0){		
				me.isWalk =	true;
				me.direction = MoveDirectionType.LEFT_UP;
			}else if(spanX<0 && spanY == 0){		
				me.isWalk =	true;
				me.direction = MoveDirectionType.LEFT;
			}else if(spanX<0 && spanY<0){		
				me.isWalk =	true;
				me.direction = MoveDirectionType.LEFT_DOWN;
			}else if(spanX == 0 && spanY<0){		
				me.isWalk =	true;
				me.direction = MoveDirectionType.DOWN;
			}else if(spanX>0 && spanY<0){		
				me.isWalk =	true;
				me.direction = MoveDirectionType.RIGHT_DOWN;
			}				
//			trace(StringUtil.substitute("X:{0},Y:{1}",spanX,spanY));
		}
	}
}